package com.imay.live.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.imay.live.R;
import com.imay.live.activity.dynamics.OnTouchListener;
import com.imay.live.heartlikeslib.swipelayout.SwipeHorizontalMenuLayout;
import com.imay.live.heartlikeslib.swipelayout.SwipeMenuLayout;
import com.imay.live.heartlikeslib.swipelayout.listener.SimpleSwipeSwitchListener;
import com.imay.live.helper.LevelHelper;
import com.imay.live.manager.AccountManager;
import com.imay.live.model.CommentModel;
import com.imay.live.model.UserModel;
import com.imay.live.utils.AppUtil;
import com.imay.live.utils.GlideUtil;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class CommentAdapter extends BaseQuickAdapter<CommentModel, BaseViewHolder> {

    public static final int VIEW_TYPE_ENABLE = 0;
    public static final int VIEW_TYPE_DISABLE = 1;

    private OnTouchListener listener;

    private long mUid;

    // #话题点击方法 采用富文本点击
    private ClickableSpan jinClick(final Context context, final String label) {
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                if (listener != null) {
                    listener.jinClick(label);
                }
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(context.getResources().getColor(R.color.text_30));
                ds.setUnderlineText(false);
                ds.setTextSize(context.getResources().getDimension(R.dimen.text_size_2x));
                ds.clearShadowLayer();
            }
        };
        return clickableSpan;
    }

    // @用户点击方法 采用富文本点击
    private ClickableSpan atClick(final Context context, final UserModel userBean) {
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                if (listener != null) {
                    listener.atClick(userBean.getNick(), userBean.getUid());
                }
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(context.getResources().getColor(R.color.text_30));
                ds.setUnderlineText(false);
                ds.setTextSize(context.getResources().getDimension(R.dimen.text_size_2x));
                ds.clearShadowLayer();
            }
        };
        return clickableSpan;
    }

    public CommentAdapter(long uid) {
        super(R.layout.item_dynamics_comment);
        mUid = uid;
    }

    @Override
    protected void convert(final BaseViewHolder helper, CommentModel item) {
        Context context = helper.getConvertView().getContext();
        if (item.getUser() == null) {
            return;
        }
        SwipeHorizontalMenuLayout menuLayout = helper.getView(R.id.sml_comment);
        menuLayout.setSwipeListener(new SimpleSwipeSwitchListener() {
            @Override
            public void endMenuOpened(SwipeMenuLayout swipeMenuLayout) {
                if (listener != null) {
                    listener.beginMenuOpened(helper.getLayoutPosition());
                }
            }

            @Override
            public void endMenuClosed(SwipeMenuLayout swipeMenuLayout) {
                if (listener != null) {
                    listener.endMenuClosed(helper.getLayoutPosition());
                }
            }
        });
        int vipRid = LevelHelper.getVIPIcon(item.getUser().getVip());
        helper.setImageResource(R.id.iv_item_comment_vip, vipRid);

        helper.setText(R.id.tv_comment_name, item.getUser().getNick());
        GlideUtil.loadCircleImage((ImageView) helper.getView(R.id.civ_item_comment_img),item.getUser().getImgHead(),R.mipmap.pnl_00_head108);
        long uid = AccountManager.getInstance().getUid();
        if (mUid != uid) {
            if (uid != item.getUser().getUid()) {
                helper.setVisible(R.id.item_comment_menu2, false);
            } else {
                helper.setVisible(R.id.item_comment_menu2, true);
            }
        } else {
            helper.setVisible(R.id.item_comment_menu2, true);
        }


        // 内容
        String msg = item.getContent();
        TextView mContentDes = helper.getView(R.id.tv_comment_msg);
        set(item, context, msg, mContentDes);


        // 时间
        long localTime = System.currentTimeMillis();
        // java乘于1000
        long createTime = item.getCreateTime();
        long diff = (localTime - (createTime * 1000L));
        long days = diff / (1000 * 60 * 60 * 24);
        long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
        // TODO
        helper.setText(R.id.tv_comment_time, AppUtil.getTimeStr(context, days, hours, minutes));

        // 点击事件
        helper.addOnClickListener(R.id.civ_item_comment_img);
        helper.addOnClickListener(R.id.tv_comment_name);
        helper.addOnClickListener(R.id.item_comment_menu1);
        helper.addOnClickListener(R.id.item_comment_menu2);
        helper.addOnClickListener(R.id.sml_comment);
    }

    // 富文本
    private void set(CommentModel item, Context context, String des, TextView mContentDes) {
        Pattern pattern = Pattern.compile("@([^@#\\s]+)|#([^@#\\s]+)");
        Matcher matcher = pattern.matcher(des);

        List<UserModel> userAtBeans = item.getAtUsers();
        List<String> labels = item.getLabels();

        SpannableString string = new SpannableString(des);
        int start;
        int end = 0;
        while (matcher.find(end)) {
            boolean c = false;
            start = matcher.start();
            end = matcher.end();
            if (userAtBeans != null) {
                for (UserModel userBean : userAtBeans) {
                    String nick = "@" + userBean.getNick();
                    if (matcher.group().equals(nick)) {
                        string.setSpan(atClick(context, userBean), start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                        c = true;
                        break;
                    }
                }
            }

            // 防止循环操作相同的
            if (c) {
                continue;
            }

            if (labels != null) {
                for (String label : labels) {
                    String labelText = "#" + label;
                    if (matcher.group().equals(labelText)) {
                        string.setSpan(jinClick(context, label), start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                        break;
                    }
                }
            }
        }

        mContentDes.setText(string);
        mContentDes.setMovementMethod(LinkMovementMethod.getInstance());
    }


    public OnTouchListener getListener() {
        return listener;
    }

    public void setListener(OnTouchListener listener) {
        this.listener = listener;
    }
}
